/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.SetuBillBean;
import com.minierp.bean.SetuBillItemBean;
import com.minierp.dao.SetuBillDao;
import com.minierp.model.SetuBill;
import com.minierp.model.SetuBillItem;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class SetuBillValidator implements Validator {
	private SetuBillDao setuBillDao;

	public SetuBillDao getSetuBillDao() {
		return this.setuBillDao;
	}

	public void setSetuBillDao(SetuBillDao setuBillDao) {
		this.setuBillDao = setuBillDao;
	}

	public boolean supports(Class clazz) {
		return SetuBillBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		SetuBillBean bean = (SetuBillBean) obj;
		SetuBill form = bean.getSetuBill();
		boolean hasError = false;
		if (form.getTsId() == 0L) {
			hasError = true;
			errors.rejectValue("setuBill.tsId", "", "请选择『当前开单帐套』");
		}

		if (!hasError
				&& (bean.getCodeRule().getGenerateRule().equals("MANUAL") || bean
						.getOldId() != 0L)) {
			if (form.getBillCode() != null
					&& !form.getBillCode().trim().equals("")) {
				SetuBill sf = this.setuBillDao.get(form.getBillCode());
				if (sf != null && sf.getId() != form.getId()) {
					errors.rejectValue("setuBill.billCode", "", "『编号』已经存在");
					hasError = true;
				}
			} else {
				errors.rejectValue("setuBill.billCode", "", "请输入『编号』");
				hasError = true;
			}
		}

		SimpleDateFormat arg17 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (!hasError) {
			try {
				form.setBillDate(arg17.parse(bean.getBillDateStr() + " "
						+ bean.getBillDateHour() + ":"
						+ bean.getBillDateMinute() + ":00"));
			} catch (Exception arg15) {
				hasError = true;
				errors.rejectValue("billDateStr", "",
						"『派单日期』填写错误，请按以下格式输入：2008-01-01");
			}
		}

		if (!hasError) {
			try {
				form.setPlanDate(arg17.parse(bean.getPlanDateStr() + " "
						+ bean.getPlanDateHour() + ":"
						+ bean.getPlanDateMinute() + ":00"));
			} catch (Exception arg14) {
				hasError = true;
				errors.rejectValue("planDateStr", "",
						"『计划完工日期』填写错误，请按以下格式输入：2008-01-01");
			}
		}

		if (!hasError) {
			try {
				form.setCost(new BigDecimal(bean.getCostStr()));
			} catch (Exception arg13) {
				hasError = true;
				errors.rejectValue("costStr", "", "『工程金额』只能输入数字");
			}
		}

		if (!hasError) {
			try {
				form.setRemaCost(new BigDecimal(bean.getRemaCostStr()));
			} catch (Exception arg12) {
				hasError = true;
				errors.rejectValue("remaCostStr", "", "『余额』只能输入数字");
			}
		}

		boolean hasInput = false;
		BigDecimal zero = new BigDecimal(0);
		if (!hasError) {
			for (int i = 0; i < bean.getItems().size(); ++i) {
				SetuBillItemBean cb = (SetuBillItemBean) bean.getItems().get(i);
				SetuBillItem c = cb.getSetuBillItem();

				try {
					c.setAmount(new BigDecimal(cb.getAmountStr()));
				} catch (Exception arg16) {
					hasError = true;
					errors.rejectValue("items", "", "『数量』只能输入数字");
					break;
				}

				if (c.getAmount().compareTo(zero) != 0) {
					hasInput = true;
				}
			}
		}

		if (!hasError && !hasInput) {
			hasError = true;
			errors.rejectValue("items", "", "安装的『数量』不能全部为0");
		}

	}
}